package vista;

import javax.sound.sampled.AudioPermission;
import javax.swing.*;

import java.applet.*;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Color;
import java.awt.Button;
import java.awt.Font;
import java.awt.Rectangle;
import java.awt.Point;
import java.awt.TextArea;
import java.awt.event.*;
import java.awt.GridLayout;
import java.awt.Component;
import java.awt.GridBagLayout;
import javax.swing.GroupLayout.Alignment;
import java.awt.Panel;
import javax.swing.border.CompoundBorder;
import javax.swing.event.ChangeEvent;

import modelo.*;

import controle.*;

import java.awt.Frame;
import java.awt.ComponentOrientation;
import java.awt.Dimension;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;

public class GUIjogo {
	
	JMenuItem personagem;
	JFrame janela;
	Main mainJogo;
	String textoAntigo = "antigo";
	TextArea textArea;
	JMenu mnNewMenu;
	JMenu mnNewMenu_1;
	boolean menuAberto = false;
	ArrayList<JMenuItem> opcoesAction = new ArrayList<JMenuItem>();
	ArrayList<JMenuItem> opcoesMenu = new ArrayList<JMenuItem>();
	Timer t;
	Timer j;
	int counter;
	boolean atacou = false;
	boolean inBattle = false;
	JLabel imageFundo;

	/**
	 * @wbp.parser.entryPoint
	 */
	public GUIjogo (Main m) {
	
		mainJogo = m;
		mainJogo.cenario = m.cenario;
		mainJogo.game = m.game;
		mainJogo.aberto = m.aberto;
		
		OuvinteEdit teste = new OuvinteEdit();
		
		janela = new JFrame();
		janela.setTitle("GuitaRPG");
		janela.setVisible(true);
		janela.setExtendedState(JFrame.MAXIMIZED_BOTH);
		janela.setResizable(false);
		janela.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		JPanel panel = new JPanel();
		panel.setBorder(new CompoundBorder());
		panel.setBackground(Color.DARK_GRAY);
		janela.getContentPane().add(panel, BorderLayout.SOUTH);
		panel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 15));
		
		JMenuBar menuBar = new JMenuBar();
		menuBar.setPreferredSize(new Dimension(100, 100));
		panel.add(menuBar);
		
		mnNewMenu = new JMenu("ACTION");
		menuBar.add(mnNewMenu);
		
		JMenuBar menuBar_1 = new JMenuBar();
		menuBar_1.setPreferredSize(new Dimension(100, 100));
		panel.add(menuBar_1);
		
		mnNewMenu_1 = new JMenu("MENU");
		menuBar_1.add(mnNewMenu_1);
		
		
		personagem = new JMenuItem("Menu do Personagem");
		personagem.addActionListener(new OuvinteMenuPersonagem());
		mnNewMenu_1.add(personagem);
		
		JMenuItem saveGame = new JMenuItem("Save Game");
		saveGame.addActionListener(new OuvinteMenuSaveGame());
		mnNewMenu_1.add(saveGame);
		
		textArea = new TextArea();
		textArea.setEditable(false);
		textArea.setText(mainJogo.cenario.mostraTexto());
		JScrollPane areaScrollPane = new JScrollPane(textArea);
		areaScrollPane.setVerticalScrollBarPolicy(
		                JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
		janela.getContentPane().add(textArea, BorderLayout.EAST);
		
		t = new Timer(50, teste);
		t.start();
		
		Panel panel_1 = new Panel();
		panel_1.setBackground(Color.BLACK);
		imageFundo = new JLabel();
		imageFundo.setIcon(new ImageIcon(mainJogo.cenario.mostraImagem(mainJogo.game.principal.foco)));
		panel_1.add(imageFundo);
		janela.getContentPane().add(panel_1, BorderLayout.CENTER);
	}
	
	class OuvinteMenuPersonagem implements ActionListener{
		public void actionPerformed(ActionEvent ev) {
			if (!menuAberto && !inBattle) {
				mainJogo.game.setLocalAnterior(mainJogo.game.principal.foco);
				mainJogo.game.setTipoLocalAnterior(mainJogo.game.principal.tipoFoco);
				mainJogo.game.principal.foco = "menu";
				mainJogo.game.principal.tipoFoco = "menu";
				textArea.setText(mainJogo.game.principal.toString());
				personagem.setLabel("Sair do Menu");
				menuAberto = true;
			}
			else if (menuAberto && !inBattle){
				mainJogo.game.principal.foco = mainJogo.game.getLocalAnterior();
				mainJogo.game.principal.tipoFoco = mainJogo.game.getTipoLocalAnterior();
				personagem.setLabel("Menu do Personagem");
				textArea.setText(mainJogo.cenario.mostraTexto());
				menuAberto = false;
			}
		}
	}
	
	class OuvinteMenuActionIrPara implements ActionListener{
		public void actionPerformed(ActionEvent ev){

			if (!menuAberto) {
				if (ev.getSource() instanceof JMenuItem) {
						JMenuItem item = (JMenuItem) ev.getSource();
						String s[] = item.getLabel().split(" ");
						mainJogo.game.principal.foco = s[2];
						
						imageFundo.setIcon(new ImageIcon(mainJogo.cenario.mostraImagem(mainJogo.game.principal.foco)));
				}
			}
		}
	}
	
	class OuvinteMenuActionAtaque implements ActionListener{
		public void actionPerformed(ActionEvent ev){

			if (!menuAberto) {
				if (ev.getSource() instanceof JMenuItem) {
						JMenuItem item = (JMenuItem) ev.getSource();
						String s = item.getLabel();
						
						mainJogo.batalha.testaGolpe(s);
						
						atacou = true;
						
						
						
				}
			}
		}
	}
	
	class OuvinteBattle implements ActionListener{
		public void actionPerformed(ActionEvent ev){
			if (!menuAberto) {
				if (atacou)
				{
					atacou = false;
					
					String s = mainJogo.batalha.Battle();
					
					if (s.matches("VICTORY!")) {
						JOptionPane.showMessageDialog(null,"YOU WON AND RECEIVED " + mainJogo.batalha.calcExp() + " EXPERIENCE");
						inBattle = false;
						mainJogo.game.principal.foco = mainJogo.batalha.getLocalAnterior();
						mainJogo.game.principal.tipoFoco = "lugar";
						imageFundo.setIcon(new ImageIcon(mainJogo.cenario.mostraImagem(mainJogo.game.principal.foco)));
	
					}
					else if (s.matches("DEFEAT!")) {
					
						JOptionPane.showMessageDialog(null,"You are now returning to the first screen","YOU LOST!",JOptionPane.INFORMATION_MESSAGE);
						inBattle = false;
						
						janela.setVisible(false);
						janela.dispose();
						new GUIinicio(mainJogo);
					}
					else {
						textArea.setText(s);
					}
				}
			}
		}
	}
	
	class OuvinteMenuSaveGame implements ActionListener{
		public void actionPerformed(ActionEvent ev){

			if (!menuAberto) {
				mainJogo.game.saveGame();
				JOptionPane.showMessageDialog(null,"Seu jogo foi salvo","Salvando",JOptionPane.INFORMATION_MESSAGE);
		
			}
		}
	}
	
	class OuvinteEdit implements ActionListener{
		public void actionPerformed(ActionEvent ev){
			if (!menuAberto) {
				if(mainJogo.cenario.alterouTexto == true && !textoAntigo.matches(mainJogo.cenario.texto)) {
					textoAntigo = mainJogo.cenario.texto;
					
					opcoesAction.clear();
					mnNewMenu.removeAll();
					
					if (mainJogo.game.principal.tipoFoco.matches("lugar")) {
					
						textArea.setText(mainJogo.cenario.mostraTexto());
						
						for (Lugar k : mainJogo.game.listaLugaresProximos()) {
							opcoesAction.add(new JMenuItem("Ir para " + k.nome));
						}
						
						counter = 0;
						
						for (JMenuItem j : opcoesAction) {
							mnNewMenu.insert(j, counter);
							j.addActionListener(new OuvinteMenuActionIrPara());
							counter++;
						}
					}
					else if (mainJogo.game.principal.tipoFoco.matches("inimigo")) {
					
						for (String k : mainJogo.game.principal.golpes) {
							opcoesAction.add(new JMenuItem(k));
						}
						
						counter = 0;
						
						for (JMenuItem j : opcoesAction) {
							mnNewMenu.insert(j, counter);
							j.addActionListener(new OuvinteMenuActionAtaque());
							counter++;
						}
						
						if (!inBattle){
						
							textArea.setText(mainJogo.cenario.mostraTexto());
							inBattle = true;
							JOptionPane.showMessageDialog(null,"A WILD ENEMY APPEARS","ALERT! ALERT! ALERT!",JOptionPane.INFORMATION_MESSAGE);
							imageFundo.setIcon(new ImageIcon(mainJogo.cenario.mostraImagem(mainJogo.game.principal.foco)));
						
							mainJogo.batalha.BattleStart(mainJogo.game.principal.foco);
							j = new Timer(1000, new OuvinteBattle());
							j.start();
						}
					}
				}
			}
		}
	}
}


